//
//  WeatherView.m
//  FastDoctor
//
//  Created by 李通 on 16/7/6.
//  Copyright © 2016年 sc. All rights reserved.
//

#import "WeatherView.h"
#import <UIImageView+WebCache.h>

@interface WeatherView ()

@property (nonatomic,weak) UIView *lineView; //分割线

@property (nonatomic,weak) UIImageView *weatherImageView; //天气的图片展示

@property (nonatomic,weak) UILabel *weatherLabel; //天气的文字描述

@property (nonatomic,weak) UIButton *cityBtn; //当前是哪个城市

@property (nonatomic,weak) UILabel *tempLabel; //今日温度

@property (nonatomic,weak) UILabel *dateLabel; //当前日期

@end

@implementation WeatherView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupWeatherViewContent];
    }
    return self;
}

#pragma mark - 初始化自定义控件
- (void)setupWeatherViewContent {
    
    self.backgroundColor = RGBCOLOR(179, 237, 228);
    
     //天气图标
    UIImageView *weatherImageView = [[UIImageView alloc]init];
    self.weatherImageView = weatherImageView;
    [self addSubview:weatherImageView];
    
    //天气的文字描述
    UILabel *weatherLabel = [[UILabel alloc]init];
    self.weatherLabel = weatherLabel;
    weatherLabel.numberOfLines = 2;
    weatherLabel.textColor = RGBCOLOR(60, 180, 185);
    weatherLabel.font = SCREEN_WIDTH == 320 ? FONT(13) : FONT(17);
    
    [self addSubview:weatherLabel];
    
    //分割线
    UIView *lineView = [[UIView alloc]init];
    self.lineView = lineView;
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:lineView];
    
    //当前城市
    UIButton *cityBtn = [[UIButton alloc]init];
    self.cityBtn = cityBtn;
    cityBtn.lineBreakMode = 0;
    [cityBtn addTarget:self
                action:@selector(didClickCityBtn)
      forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:cityBtn];
    
    //今日温度
    UILabel *tempLabel = [[UILabel alloc]init];
    
    self.tempLabel = tempLabel;
    
    tempLabel.font = SCREEN_WIDTH == 320 ? FONT(13) : FONT(17);
    tempLabel.textColor = RGBCOLOR(60, 180, 185);
    
    [self addSubview:tempLabel];
    
    //当前日期
    UILabel *dateLabel = [[UILabel alloc]init];
    self.dateLabel = dateLabel;
    
    dateLabel.font = SCREEN_WIDTH == 320 ? FONT(13) : FONT(17);
    dateLabel.textColor = RGBCOLOR(60, 180, 185);
    
    [self addSubview:dateLabel];
    
    [self setupWeatherViewConstraints];
}

//为按钮设置副本文属性
- (void)setupCityBtnWithString:(NSString *)string {
    
    //设置字体颜色
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:RGBCOLOR(60, 180, 185),NSForegroundColorAttributeName,[NSNumber numberWithInt:NSUnderlineStyleSingle],NSUnderlineStyleAttributeName,SCREEN_WIDTH == 320 ? FONT(13) : FONT(17),NSFontAttributeName, nil];
    //创建富文本对象
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:string attributes:attributes];
    
    [_cityBtn setAttributedTitle:str forState:UIControlStateNormal];
}

//点击城市按钮跳转
- (void)didClickCityBtn {
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"city" object:nil];
}

#pragma mark - 为控件赋值
- (void)setWeatherDict:(NSDictionary *)weatherDict {
    
    _weatherDict = weatherDict;
    
    
    if ([weatherDict  isEqual: @{}])
    {
        
        self.tempLabel.text = @"";
        self.dateLabel.text = @"";
        self.weatherLabel.text = @"";
        self.weatherImageView.image = [UIImage imageNamed:@"good_image_default"];
        return;
    }

    
    self.tempLabel.text = [NSString stringWithFormat:@"温度: %@",weatherDict[@"temperature"]];
    
    NSString *str = [weatherDict[@"date"] substringWithRange:NSMakeRange(3, 6)];
    
    self.dateLabel.text = [NSString stringWithFormat:@"日期: %@",str];

    self.weatherLabel.text = weatherDict[@"weather"];
    
    [self.weatherImageView sd_setImageWithURL:weatherDict[@"dayPictureUrl"]];
}

- (void)setCityStr:(NSString *)cityStr {
    
    _cityStr = cityStr;
    
    [self setupCityBtnWithString:cityStr];
}

#pragma mark - 为控件布局
- (void)setupWeatherViewConstraints {
    
    CGFloat iconW = SCREEN_WIDTH == 320 ? 40 : 50;
    CGFloat iconH = iconW - 10;
    [_weatherImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.mas_centerY);
        make.width.mas_equalTo(iconW);
        make.height.mas_equalTo(iconH);
        make.leading.equalTo(self).offset(10);
        
    }];
    
    CGFloat w = SCREEN_WIDTH == 320 ? 40 : 65;
    CGFloat h = SCREEN_HEIGHT == 568 ? 40 : 50;
    [_weatherLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self);
        make.width.mas_equalTo(w);
        make.height.mas_equalTo(h);
        make.leading.equalTo(_weatherImageView.mas_trailing).offset(10);
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(self);
        make.top.equalTo(self).offset(10);
        make.bottom.equalTo(self).offset(-10);
        make.width.mas_equalTo(2);
    }];
    
    [_cityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(_weatherLabel.trailing);
        make.trailing.equalTo(_lineView.mas_leading).offset(-10);
        make.centerY.equalTo(self);
    }];
    
    CGFloat marh = SCREEN_HEIGHT == 568 ? 15 : 25;
    [_tempLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self).offset(marh);
        make.trailing.equalTo(self).offset(-40);
    }];
    
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self).offset(-marh);
        make.leading.equalTo(_tempLabel);
    }];
}

@end
